package routefinder.module;

// Location.java
// Written by Jiayuan
// class to represent a location in the path
// each location object can be linked to another
// location object to form a path

public class Location {
	private String locationName;
	private double distance;
	private Location next;

	// constructor for adding 1 location
	public Location(String location) {
		this(location, 0, null);
	}

	public Location(String location, double distance) {
		this(location, distance, null);
	}

	// constructor for adding a path
	public Location(String location, double distance, Location next) {
		this.locationName = location;
		this.distance = distance;
		this.next = next;
	}

	public String getLocationName() {
		return locationName;
	}

	public void setLocationName(String location) {
		locationName = location;
	}

	public void setNextLocation(Location location) {
		if (next != null) {
			// add the new location to the end of the list
			Location temp = next;
			while (temp.next != null)
				temp = temp.getNextLocation();
			temp.next = location;
		} else
			next = location;
	}

	public Location getNextLocation() {
		return next;
	}

	// get the next location object in the path
	public Location getNextLocation(Location end) {
		Location temp = next;
		while (temp != null) {
			if (temp.equals(end))
				return temp;
			temp = temp.getNextLocation();
		}
		return null;
	}

	public double getDistance() {
		return distance;
	}

	public void setDistance(double distance) {
		this.distance = distance;
	}

	public boolean equals(Object obj) {
		return obj instanceof Location
				&& this.locationName.equalsIgnoreCase(((Location) obj).getLocationName());
	}
} // end class Location
